<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Partner;
use Illuminate\Http\Request;

class PartnerController extends BaseController
{
    public $model = Partner::class;

    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $users = Partner::when($start_at, function ($q) use($start_at){
            $q->where('created_at', '>=', $start_at);
        })->when($end_at, function ($q) use($end_at){
            $end_at = date('Y-m-d', strtotime($end_at) + 24 * 3600);
            $q->where('created_at', '<=', $end_at);
        })->orderBy('sort', 'desc')->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($users);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:partner'
        ]);
        $id = \request("id");
        $info = Partner::find($id);
        return $this->success($info);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'file_path' => 'required',
            'sort' => 'filled|integer',
            'id' => 'filled|exists:partner'
        ]);
        $input = $request->input();
        $id = \request("id");
        if ($id) {
            $Partner = Partner::where('id', $id)->first();
            $Partner->update($input);
        } else {
            $info = Partner::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }



}

